﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;


public class UserService
{
    private static UserService instance = null;

    List<User> users = new List<User>();

    private UserService()
	{
        users.Add(new User("Jens", "jens@jens.dk"));
        users.Add(new User("Peter", "peter@peter.dk"));
        users.Add(new User("Hans", "hans@hans.dk"));
        users.Add(new User("Kent", "kent@kent.dk"));
        users.Add(new User("Ruben", "ruben@ruben.dk"));
        users.Add(new User("Knud", "knud@knud.dk"));
	}

    public static UserService getInstance()
    {
        if (instance == null)
        {
            instance = new UserService();
        }
        return instance;
    }

    public String doLogin(String email, String password)
    {
        User user = UserFactory.getInstance().loginUser(email, password);
        if (user != null)
        {
            return SessionService.getInstance().createSession(user).ToString();
        }
        else
        {
            return loginFailed("Email or password incorrect");
        }
    }

    public String loginFailed(String message)
    {
        DelegateService.getInstance().getResponse().StatusCode = 401;
        return message;
    }

    public String createUser(String email, String password, String name)
    {
        UserFactory.getInstance().createUser(email, password, name);
        return doLogin(email, password);
    }

    public List<Project> getProjectsForUser(int userId)
    {
        return ProjectFactory.getInstance().getProjectsForUser(userId);
    }
}
